package com.resou.reader.mine.presenter;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.service.UserService;
import com.resou.reader.base.m.IModel;
import com.resou.reader.base.p.BasePresenter;
import com.resou.reader.commom.Constant;
import com.resou.reader.utils.log.DLog;
import com.resou.reader.mine.IView.IPhoneBindView;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.utils.ErrorUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ${Garrett} on 2018/9/20.
 * Contact me krouky@outlook.com
 */
public class PhoneBindPresenter extends BasePresenter<IPhoneBindView, IModel> {
    private static final String TAG = "PhoneBindPresenter";
    private UserService mService;

    public PhoneBindPresenter(IPhoneBindView view) {
        this.view = view;
        mService = ApiImp.getInstance().getService(UserService.class);
    }

    public void sendSMSCode(String phone) {
        mService.smsSend(phone, Constant.SMS_SEND_TYPE_BINDMOBILE).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .as(this.<Result>bindLifecycle())
                .subscribe(new Consumer<Result>() {
                    @Override
                    public void accept(Result result) throws Exception {
                        DLog.i(TAG, "sendSMSCode  success, msg== " + result.getCode());
                        view.toast(result.getMsg());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DLog.i(TAG, "sendSMSCode  error, msg== " + throwable.getMessage());
                        throwable.printStackTrace();
                    }
                });
    }

    public void bindMobile(final String number, String code) {
        mService.bindMobile(number, code, UserInstance.getToken())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .as(this.<Result>bindLifecycle())
                .subscribe(new Consumer<Result>() {
                    @Override
                    public void accept(Result result) throws Exception {
                        DLog.i(TAG, "bindMobile  success ,code :" + result.getCode());
                        if (result.getCode() == 0) {
                            UserInstance.getLoginData().setPhone(number);
                            view.bindSuccess();
                        } else {
                            view.bindError(result.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DLog.i(TAG, "bindMobile  error, msg :" + throwable.getMessage());
                        ErrorUtils.showError(throwable);
                    }
                });
    }
}
